package com.rongshu.view.discovery.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.GlideUtils;
import com.rongshu.utils.LoginUtils;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.home.bean.RecommendAuthorResponse;
import com.rongshu.view.home.presenter.DiscoveryPresenter;

import java.util.List;

/**
 * created by zhengweis on 2018/11/1.
 */
public class RecommendAuthorAdapter extends RecyclerView.Adapter<RecommendAuthorAdapter.ViewHolder> {
    private Context context;
    private List<RecommendAuthorResponse.ResponseBean> list;
    private AdapterListener listener;

    public RecommendAuthorAdapter(Context context, List<RecommendAuthorResponse.ResponseBean> list) {
        this.list = list;
        this.context = context;
    }

    public void setAdapterListener(AdapterListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_recommend_author, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        viewHolder.vip.setVisibility(list.get(i).getVip() ? View.VISIBLE : View.GONE);
        GlideUtils.loadImageView(context, viewHolder.authorImg, list.get(i).getAvatar(), R.drawable.user_head_icon, R.drawable.user_head_icon);
        String bi_name = list.get(i).getBi_name();
        if (bi_name.length() > 4){
            viewHolder.authorName.setText(bi_name.substring(0, 4) + "...");
        } else {
            viewHolder.authorName.setText(bi_name);
        }
        viewHolder.authorImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    if (UserInfoUtils.getUserInfo() != null){
                        listener.callBack(list.get(i));
                    } else {
                        LoginUtils.toLogin(context);
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView authorName;
        private ImageView authorImg, vip;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            authorImg = itemView.findViewById(R.id.author_img);
            vip = itemView.findViewById(R.id.vip_flag);
            authorName = itemView.findViewById(R.id.author_name);
        }
    }
}
